package cn.sinata.wanglin.utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.village.activity.BNGuideActivity;


public class BdNaviMapUtil{
    public static final String ROUTE_PLAN_NODE = "routePlanNode";
    Context mContext;
    private String mSDCardPath = null;
    String authinfo = null;
    private static final String TAG = "DAOHANGLOG";
    private static final String APP_FOLDER_NAME = "wanglin";
    public BdNaviMapUtil(Activity mActivity){
        if (initDirs()) {
            initNavi(mActivity);
        }else{
            Log.e("存储不足---》", "存储不足");
        }


    }

    private boolean initDirs() {
        mSDCardPath = getSdcardDir();
        if (mSDCardPath == null) {
            return false;
        }
        File f = new File(mSDCardPath, APP_FOLDER_NAME);
        if (!f.exists()) {
            try {
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }


    private void initNavi(Activity mActivity) {
        BaiduNaviManager.getInstance().init(mActivity, mSDCardPath, APP_FOLDER_NAME, new BaiduNaviManager.NaviInitListener() {
            @Override
            public void onAuthResult(int status, String msg) {
                if (0 == status) {
                    authinfo = "key校验成功!";
                } else {
                    authinfo = "key校验失败, " + msg;
                }
                Log.e("导航key校验--->", "" + authinfo);
            }

            public void initSuccess() {
                Log.e(TAG, "百度导航引擎初始化成功");
            }

            public void initStart() {
                Log.e(TAG, "百度导航引擎初始化开始");

            }

            public void initFailed() {
                Log.e(TAG, "百度导航引擎初始化失败");

            }


        }, null,null,null);

    }
    public static void routeplanToNavi(Activity mActivity,double startLongitude, double startLatitude,double stopLongitude, double stopLatitude,CallBack mCallBack) {

        BNRoutePlanNode sNode = null;
        BNRoutePlanNode eNode = null;
        sNode = new BNRoutePlanNode(startLongitude, startLatitude, null, null, BNRoutePlanNode.CoordinateType.BD09LL);
        eNode = new BNRoutePlanNode(stopLongitude, stopLatitude, null, null, BNRoutePlanNode.CoordinateType.BD09LL);
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(mActivity, list, 1, true, new DemoRoutePlanListener(sNode,mActivity,mCallBack));
        }
    }
    public static class DemoRoutePlanListener implements BaiduNaviManager.RoutePlanListener {

        private BNRoutePlanNode mBNRoutePlanNode = null;
        Activity mActivity;
        CallBack mCallBack;
        public DemoRoutePlanListener(BNRoutePlanNode node,Activity mActivity,CallBack mCallBack) {
            mBNRoutePlanNode = node;
            this.mActivity=mActivity;
            this.mCallBack=mCallBack;
        }

        @Override
        public void onJumpToNavigator() {
			/*
			 * 设置途径点以及resetEndNode会回调该接口
			 */

            if (mActivity.getClass().getName().endsWith("BNGuideActivity")) {
                mCallBack.onResult(1,null,null);
                return;
            }
            mCallBack.onResult(0,null,null);
            Intent intent = new Intent(mActivity, BNGuideActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
            intent.putExtras(bundle);
            mActivity.startActivity(intent);

        }

        @Override
        public void onRoutePlanFailed() {
            // TODO Auto-generated method stub
            mCallBack.onResult(1,null,null);
        }
    }
}
